home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1997 January
/
Macworld (1997-01).dmg
/
Shareware World
/
Utilities
/
Data & Time Management
/
MacCalendar
/
Src
/
MacCalendarSetup.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-10-20
|
16KB
|
726 lines
/* MacCalendarSetup.c */
/*
* MacCalendarSetup.c
* Copyright © 1993-94 Apple Computer Inc. All rights reserved.
* Set the configuration parameters for MacCalendar
*
* File Type APPL
* File Creator SCCF -- registered with DTS
*/
#ifndef SystemSevenOrLater
#define SystemSevenOrLater 1
#endif
#include <Dialogs.h>
#include <Errors.h>
#include <Fonts.h>
#include <Memory.h>
#include <Menus.h>
#include <Quickdraw.h>
#include <Resources.h>
#include <SegLoad.h>
#include <ToolUtils.h>
#include <Types.h>
#include "MacCalendarSetup.h"
#ifdef __powerc
QDGlobals qd; /* This is not automatically defined on PowerPC */
#endif
MenuHandle gAppleMenu;
MenuHandle gFileMenu;
MenuHandle gEditMenu;
MenuHandle gFontMenu;
MenuHandle gFontSizeMenu;
MenuHandle gFirstDayMenu;
DialogPtr gSetupDialog;
short gAppResourceFile;
short gPrefFileRefNum;
SavedSettingsHandle gSettingHandle = NULL;
SavedSettings *gSettingPtr;
#define SET (*gSettingPtr)
DateTimeRec gNow;
void InitApplication(void);
void SetupDialog(void);
void SetThisDayName(
short itemNumber,
ConstStr255Param itemText
);
Boolean DoDialog(void);
pascal void DrawCalendarUserItem(
DialogPtr theDialog,
short itemNumber
);
pascal void DrawVersionUserItem(
DialogPtr theDialog,
short itemNumber
);
pascal void DrawDayNameUserItem(
DialogPtr theDialog,
short itemNumber
);
void RebuildDayNameText(void);
void AddThisDayName(
short dayNumber
);
OSErr ReadCurrentParameters(
SignedByte permission
);
void SaveNewParameters(void);
OSErr OpenPreferenceFile(
OSType prefFileType,
OSType prefFileCreator,
SignedByte permission,
short *prefVRefNum,
StringPtr prefFileName
);
void NonFatalError(
OSErr errorStatus,
short errorMsgID
);
void FatalError(
OSErr errorStatus,
short errorMsgID
);
void ErrorAlert(
short alertID,
OSErr errorStatus,
short errorMsgID
);
void
main(void)
{
unsigned long nowSeconds;
Boolean updateNeeded;
InitApplication();
GetDateTime(&nowSeconds);
Secs2Date(nowSeconds, &gNow);
gSetupDialog = GetNewDialog(DLOG_Setup, NULL, (WindowPtr) -1L);
if (gSetupDialog != NULL) {
if (ReadCurrentParameters(fsRdWrPerm) != noErr) {
DisposeDialog(gSetupDialog);
NoteAlert(ALRT_NoPreferences, NULL);
}
else {
SetupDialog();
updateNeeded = DoDialog();
DisposeDialog(gSetupDialog);
if (updateNeeded
&& gPrefFileRefNum != 0
&& NoteAlert(ALRT_WaitForReboot, NULL) == kOKButton)
SaveNewParameters();
if (gPrefFileRefNum != 0)
CloseResFile(gPrefFileRefNum);
}
}
ExitToShell();
}
Boolean
DoDialog(void)
{
short itemHit;
short itemType;
Handle itemHandle;
Rect itemRect;
short i;
Str255 work;
Boolean redraw;
long newFontSize;
ShowWindow(gSetupDialog);
do {
ModalDialog(NULL, &itemHit);
GetDItem(
gSetupDialog,
itemHit,
&itemType,
&itemHandle,
&itemRect
);
redraw = FALSE;
switch (itemHit) {
case kFontNamePopupItem:
i = GetCtlValue((ControlHandle) itemHandle);
GetItem(gFontMenu, i, work);
if (EqualString(SET.fontName, work, FALSE, FALSE) == FALSE) {
pstrcpy(SET.fontName, work);
redraw = TRUE;
}
break;
case kFontSizePopupItem:
i = GetCtlValue((ControlHandle) itemHandle);
GetItem(gFontSizeMenu, i, work);
StringToNum(work, &newFontSize);
if (newFontSize != SET.fontSize) {
SET.fontSize = newFontSize;
redraw = TRUE;
}
break;
case kFirstDayPopupItem:
i = GetCtlValue((ControlHandle) itemHandle);
if (i != SET.firstDayOfWeek) {
SET.firstDayOfWeek = i;
redraw = TRUE;
}
break;
#if DEBUG
/*
* This lets us test the drawing algorithm without rebuilding the
* actual status bar and rebooting the machine.
*/
case kPrevMonth:
if (--gNow.month < 1) {
gNow.month = 12;
--gNow.year;
}
redraw = TRUE;
break;
case kNextMonth:
if (++gNow.month > 12) {
gNow.month = 1;
++gNow.year;
}
redraw = TRUE;
break;
#endif
default:
if (itemHit >= kSundayText && itemHit <= kSaturdayText)
redraw = TRUE;
break;
}
if (redraw)
DrawCalendarUserItem(gSetupDialog, kCalendarUserItem);
} while (itemHit != kOKButton && itemHit != kCancelButton);
return (itemHit == kOKButton);
}
pascal void
DrawDayNameUserItem(
DialogPtr theDialog,
short itemNumber
)
{
short itemType;
Handle itemHandle;
Rect itemRect;
Rect dayNameRect;
short i;
Str255 work;
Intl1Rec **itlHandle;
GrafPtr savePort;
short saveFontNumber;
short saveFontSize;
GetPort(&savePort);
SetPort(theDialog);
saveFontNumber = theDialog->txFont;
saveFontSize = theDialog->txSize;
GetDItem(
theDialog,
itemNumber,
&itemType,
&itemHandle,
&dayNameRect
);
itlHandle = (Intl1Rec **) IUGetIntl(1);
for (i = 0; i < 7; i++) {
/*
* Get the string, abbreviate it, and draw it in the
* day name rectangle, tabbed over the associated
* edit text area. Messy, but it looks nicer.
*/
pstrcpy(work, (**itlHandle).days[i]);
GetDItem(
theDialog,
kSundayText + i,
&itemType,
&itemHandle,
&itemRect
);
itemRect.top = dayNameRect.top;
itemRect.bottom = dayNameRect.bottom;
TextBox(
&work[1],
(**itlHandle).abbrLen,
&itemRect,
teJustLeft
);
}
TextFont(saveFontNumber);
TextSize(saveFontSize);
SetPort(savePort);
}
pascal void
DrawCalendarUserItem(
DialogPtr theDialog,
short itemNumber
)
{
short itemType;
Handle itemHandle;
Rect itemRect;
short fontNumber;
GrafPtr savePort;
short saveFontNumber;
short saveFontSize;
RgnHandle saveRgn;
RebuildDayNameText();
GetPort(&savePort);
SetPort(theDialog);
saveFontNumber = theDialog->txFont;
saveFontSize = theDialog->txSize;
GetDItem(
theDialog,
itemNumber,
&itemType,
&itemHandle,
&itemRect
);
EraseRect(&itemRect);
FrameRect(&itemRect);
GetFNum(SET.fontName, &fontNumber);
saveRgn = NewRgn();
GetClip(saveRgn);
ClipRect(&itemRect);
DrawCalendar(
gNow.year,
gNow.month,
SET.firstDayOfWeek,
SET.dateString,
&itemRect,
fontNumber,
SET.fontSize
);
SetClip(saveRgn);
DisposeRgn(saveRgn);
TextFont(saveFontNumber);
TextSize(saveFontSize);
SetPort(savePort);
}
pascal void
DrawVersionUserItem(
DialogPtr theDialog,
short itemNumber
)
{
short itemType;
Handle itemHandle;
Rect itemRect;
GrafPtr savePort;
short saveFontNumber;
short saveFontSize;
FontInfo info;
GetPort(&savePort);
SetPort(theDialog);
saveFontNumber = theDialog->txFont;
saveFontSize = theDialog->txSize;
GetDItem(
theDialog,
itemNumber,
&itemType,
&itemHandle,
&itemRect
);
EraseRect(&itemRect);
TextFont(geneva);
TextSize(9);
GetFontInfo(&info);
MoveTo(itemRect.left, itemRect.top + info.ascent);
DrawString("\p" kVersionIdent);
TextFont(saveFontNumber);
TextSize(saveFontSize);
SetPort(savePort);
}
void
RebuildDayNameText(void)
{
short i;
SET.dateString[0] = 0;
if (SET.firstDayOfWeek == kFirstIsMonday) {
for (i = kMondayText; i <= kSaturdayText; i++)
AddThisDayName(i);
AddThisDayName(kSundayText);
}
else {
for (i = kSundayText; i <= kSaturdayText; i++)
AddThisDayName(i);
}
SET.dateString[++SET.dateString[0]] = 0;
}
void
AddThisDayName(
short dayNumber
)
{
short itemType;
Handle itemHandle;
Rect itemRect;
Str255 work;
GetDItem(
gSetupDialog,
dayNumber,
&itemType,
&itemHandle,
&itemRect
);
GetIText(itemHandle, work);
BlockMove(
work,
&SET.dateString[SET.dateString[0] + 1],
work[0] + 1
);
SET.dateString[0] += (work[0] + 1);
}
void
SetupDialog(void)
{
short nMenuItems;
short i;
Str255 work;
Str255 fontSizeText;
short itemType;
Handle itemHandle;
Rect itemRect;
StringPtr dayName;
#ifndef MPW /* These are not in ETO 13, but they are in Think 7.0 */
SetDialogDefaultItem(gSetupDialog, kOKButton);
SetDialogCancelItem(gSetupDialog, kCancelButton);
#endif
/*
* Set the font name popup menu
*/
nMenuItems = CountMItems(gFontMenu);
for (i = 1; i <= nMenuItems; i++) {
GetItem(gFontMenu, i, work);
if (EqualString(work, SET.fontName, FALSE, FALSE))
break;
}
if (i <= nMenuItems) {
GetDItem(
gSetupDialog,
kFontNamePopupItem,
&itemType,
&itemHandle,
&itemRect
);
SetCtlValue((ControlHandle) itemHandle, i);
}
/*
* Set the font size popup menu
*/
nMenuItems = CountMItems(gFontSizeMenu);
NumToString(SET.fontSize, fontSizeText);
for (i = 1; i <= nMenuItems; i++) {
GetItem(gFontSizeMenu, i, work);
if (EqualString(work, fontSizeText, FALSE, FALSE))
break;
}
if (i <= nMenuItems) {
GetDItem(
gSetupDialog,
kFontSizePopupItem,
&itemType,
&itemHandle,
&itemRect
);
SetCtlValue((ControlHandle) itemHandle, i);
}
/*
* Set the first day popup menu
*/
GetDItem(
gSetupDialog,
kFirstDayPopupItem,
&itemType,
&itemHandle,
&itemRect
);
SetCtlValue(
(ControlHandle) itemHandle,
SET.firstDayOfWeek
);
/*
* Set the calendar drawing procedure
*/
GetDItem(
gSetupDialog,
kCalendarUserItem,
&itemType,
&itemHandle,
&itemRect
);
SetDItem(
gSetupDialog,
kCalendarUserItem,
itemType,
(Handle) DrawCalendarUserItem,
&itemRect
);
/*
* Setup the day name user item
*/
GetDItem(
gSetupDialog,
kDayNameUserItem,
&itemType,
&itemHandle,
&itemRect
);
SetDItem(
gSetupDialog,
kDayNameUserItem,
itemType,
(Handle) NewUserItemProc(DrawDayNameUserItem),
&itemRect
);
/*
* Setup the version number user item
*/
GetDItem(
gSetupDialog,
kVersionUserItem,
&itemType,
&itemHandle,
&itemRect
);
SetDItem(
gSetupDialog,
kVersionUserItem,
itemType,
(Handle) NewUserItemProc(DrawVersionUserItem),
&itemRect
);
/*
* Move the current date string into the dialog edit records.
*/
dayName = (StringPtr) &SET.dateString[1];
i = (SET.firstDayOfWeek == kFirstIsSunday)
? kSundayText : kMondayText;
for (; i <= kSaturdayText; i++) {
SetThisDayName(i, dayName);
dayName = (StringPtr) &dayName[dayName[0] + 1];
}
if (SET.firstDayOfWeek == kFirstIsMonday)
SetThisDayName(kSundayText, dayName);
}
void
SetThisDayName(
short itemNumber,
ConstStr255Param itemText
)
{
short itemType;
Handle itemHandle;
Rect itemRect;
GetDItem(
gSetupDialog,
itemNumber,
&itemType,
&itemHandle,
&itemRect
);
SetIText(itemHandle, itemText);
}
OSErr
ReadCurrentParameters(
SignedByte permission
)
{
OSErr status;
Str255 work;
short saveResFile;
long tempLong;
Str255 prefResourceName;
GetIndString(prefResourceName, STRN_Strings, kStrPrefName);
saveResFile = CurResFile();
status = OpenPreferenceFile(
kPrefFileType,
kPrefFileCreator,
permission,
&gPrefFileRefNum,
work
);
if (status == noErr) {
UseResFile(gPrefFileRefNum);
gSettingHandle = (SavedSettingsHandle) Get1NamedResource(
kPrefResourceType,
prefResourceName
);
if (gSettingHandle != NULL) {
/*
* Make sure we're sailing on the same ship.
*/
MoveHHi((Handle) gSettingHandle);
HLock((Handle) gSettingHandle);
gSettingPtr = (*gSettingHandle);
if (GetHandleSize((Handle) gSettingHandle) != sizeof (SavedSettings)
|| SET.signature != kApplicationCreator
|| SET.prefVersion != kPrefVersion
|| SET.firstDayOfWeek < kFirstIsSunday
|| SET.firstDayOfWeek > kFirstIsMonday) {
RmveResource((Handle) gSettingHandle);
DisposeHandle((Handle) gSettingHandle);
gSettingHandle = NULL;
}
}
if (gSettingHandle == NULL) {
/*
* Build a default SavedSettings record.
*/
gSettingHandle = (SavedSettingsHandle)
NewHandleClear(sizeof (SavedSettings));
if (gSettingHandle == NULL)
status = MemError();
else {
MoveHHi((Handle) gSettingHandle);
HLock((Handle) gSettingHandle);
gSettingPtr = (*gSettingHandle);
SET.signature = kApplicationCreator;
SET.prefVersion = kPrefVersion;
UseResFile(saveResFile);
GetIndString(SET.fontName, STRN_Strings, kStrFontName);
GetIndString(work, STRN_Strings, kStrFontSize);
StringToNum(work, &tempLong);
SET.fontSize = tempLong;
GetIndString(work, STRN_Strings, kStrFirstDayOfWeek);
StringToNum(work, &tempLong);
SET.firstDayOfWeek = tempLong;
GetIndString(SET.dateString, STRN_Strings, kStrDayName);
if (gPrefFileRefNum != 0) {
UseResFile(gPrefFileRefNum);
AddResource(
(Handle) gSettingHandle,
kPrefResourceType,
UniqueID(kPrefResourceType),
prefResourceName
);
if (ResError() == noErr)
WriteResource((Handle) gSettingHandle);
}
}
}
}
UseResFile(saveResFile);
return (status);
}
void
SaveNewParameters(void)
{
short saveResFile;
OSErr status;
saveResFile = CurResFile();
UseResFile(gPrefFileRefNum);
status = ResError();
if (status == noErr)
ChangedResource((Handle) gSettingHandle);
status = ResError();
if (status == noErr)
UpdateResFile(gPrefFileRefNum);
status = ResError();
if (status != noErr)
NonFatalError(status, kMsgWritingPrefResource);
UseResFile(saveResFile);
}
void
InitApplication(void)
{
Handle menuBarHdl;
MaxApplZone();
InitGraf(&qd.thePort);
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs(0);
HNoPurge((Handle) GetCursor(watchCursor));
SetCursor(*GetCursor(watchCursor));
menuBarHdl = GetNewMBar(MBAR_MenuBar);
SetMenuBar(menuBarHdl);
gAppleMenu = GetMHandle(MENU_Apple);
gFileMenu = GetMHandle(MENU_File);
gEditMenu = GetMHandle(MENU_Edit);
gFirstDayMenu = GetMHandle(MENU_FirstDay);
AddResMenu(GetMHandle(MENU_Apple), 'DRVR');
DrawMenuBar();
gFontMenu = GetMenu(MENU_Font);
AddResMenu(gFontMenu, 'FONT');
gFontSizeMenu = GetMenu(MENU_FontSize);
gAppResourceFile = CurResFile();
InitCursor();
}
void
NonFatalError(
OSErr errorStatus,
short errorMsgID
)
{
ErrorAlert(ALRT_NonFatalError, errorStatus, errorMsgID);
}
void
FatalError(
OSErr errorStatus,
short errorMsgID
)
{
ErrorAlert(ALRT_FatalError, errorStatus, errorMsgID);
}
void
ErrorAlert(
short alertID,
OSErr errorStatus,
short errorMsgID
)
{
Handle errorTextHdl;
Str15 errorStatusText;
short userChoice;
Str255 errorMsgText;
Str255 errorText;
short saveResFile;
saveResFile = CurResFile();
UseResFile(gAppResourceFile);
GetIndString(errorMsgText, STRN_Messages, errorMsgID);
NumToString(errorStatus, errorStatusText);
errorTextHdl = GetResource('Estr', errorStatus);
if (errorTextHdl != NULL)
pstrcpy(errorText, *errorTextHdl);
else {
GetIndString(errorText, STRN_Messages, kMsgSystemError);
}
ParamText(errorStatusText, errorText, errorMsgText, "\p");
userChoice = StopAlert(alertID, NULL);
UseResFile(saveResFile);
if (alertID == ALRT_FatalError || userChoice == kOKButton)
ExitToShell();
}